//
//  M1SheetView.m
//  NewMarryU
//
//  Created by apple on 2018/7/13.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1SheetView.h"
#import "M1SheetCell.h"
@implementation M1SheetView
-(instancetype)initWithTitles:(NSArray *)titles{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
        _m1_titles = titles;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        _m1_bgView = [[UIView alloc]init];
        _m1_bgView.frame = CGRectMake(20, SCREENHEIGHT, SCREENWIDTH-40, _m1_titles.count*55+70);
        [self addSubview:_m1_bgView];

        _m1_table = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _m1_table.delegate = self;
        _m1_table.dataSource = self;
        _m1_table.scrollEnabled = NO;
        _m1_table.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _m1_table.estimatedSectionFooterHeight = 0;
        _m1_table.estimatedSectionHeaderHeight = 0;
        _m1_table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _m1_table.separatorColor = m1_line_color;
        _m1_table.rowHeight = 55;
        _m1_table.tableFooterView = [[UIView alloc]init];
        _m1_table.layer.cornerRadius = 13;
        [_m1_bgView addSubview:_m1_table];
        [_m1_table mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(0);
            make.right.offset(0);
            make.height.mas_offset(_m1_titles.count*55);
            make.top.offset(0);
        }];
        
        [_m1_table registerNib:[UINib nibWithNibName:NSStringFromClass([M1SheetCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
        UIButton *m1_cancel = [UIButton buttonWithType:UIButtonTypeCustom];
        [m1_cancel setBackgroundImage:[UIImage imageNamed:@"btn_color"] forState:0];
        m1_cancel.layer.cornerRadius = 13;
        m1_cancel.clipsToBounds = YES;
        [m1_cancel setTitle:@"取消" forState:0];
        [m1_cancel setTitleColor:[UIColor whiteColor] forState:0];
        m1_cancel.titleLabel.font = m1_font(18);
        [m1_cancel addTarget:self action:@selector(m1_diss) forControlEvents:UIControlEventTouchUpInside];
        [_m1_bgView addSubview:m1_cancel];
        [m1_cancel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(0);
            make.right.offset(0);
            make.height.mas_offset(55);
            make.top.equalTo(_m1_table.mas_bottom).offset(15);
            make.bottom.offset(0);
        }];
    }
    return self;
}
-(void)m1_diss{
    [UIView animateWithDuration:0.27 animations:^{
        _m1_bgView.frame = CGRectMake(20, SCREENHEIGHT, SCREENWIDTH-40, _m1_titles.count*55+70);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self m1_diss];
}
-(void)m1_show{

    [[UIApplication sharedApplication].delegate.window addSubview:self];

    [UIView animateWithDuration:0.27 animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        _m1_bgView.frame = CGRectMake(20, SCREENHEIGHT-_m1_titles.count*55-70-(iPhoneX?34:10), SCREENWIDTH-40, _m1_titles.count*55+70);
       
    }];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [UIView animateWithDuration:0.27 animations:^{
        _m1_bgView.frame = CGRectMake(20, SCREENHEIGHT, SCREENWIDTH-40, _m1_titles.count*55+70);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (_click) {
            _click(indexPath.row);
        }
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _m1_titles.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    M1SheetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.m1_titleLB.text = _m1_titles[indexPath.row];
    return cell;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
